package io.rehuo.modules.app.utils;

import com.alibaba.fastjson.JSONObject;
import io.rehuo.common.exception.RRException;
import io.rehuo.common.utils.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.SSLContext;
import java.io.*;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Map;

public class HttpCommonClientUtils {

    private static PoolingHttpClientConnectionManager pccm = null;

    private static final Logger logger = LoggerFactory.getLogger(HttpCommonClientUtils.class);

    private static final String CONTENT_TYPE = "application/json;charset=utf-8";

    static {
        try {
            //设置访问协议
            SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                //信任所有
                public boolean isTrusted(X509Certificate[] chain,
                                         String authType) throws CertificateException {
                    return true;
                }
            }).build();
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
            Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
                    .register("http", PlainConnectionSocketFactory.getSocketFactory())
                    .register("https", sslsf)
                    .build();

            pccm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
            pccm.setDefaultMaxPerRoute(30); //每个主机的最大并行链接数
            pccm.setMaxTotal(200);
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取连接
     *
     * @return
     */
    private static HttpClient getHttpClient() {

        //设置连接超时时间
        int REQUEST_TIMEOUT = 20 * 1000;  //设置请求超时20秒钟
        int SO_TIMEOUT = 20 * 1000;       //设置等待数据超时时间20秒钟
        RequestConfig defaultRequestConfig = RequestConfig.custom()
                .setSocketTimeout(SO_TIMEOUT)
                .setConnectTimeout(REQUEST_TIMEOUT)
                .setConnectionRequestTimeout(REQUEST_TIMEOUT)
                .setStaleConnectionCheckEnabled(true)
                .build();

        CloseableHttpClient httpClient = HttpClients.custom()
                .setConnectionManager(pccm).setDefaultRequestConfig(defaultRequestConfig).build();
        return httpClient;
    }

    /**
     * 发起请求并返回结果POST
     *
     * @param url
     * @param params
     * @return
     * @throws Exception
     */
    public static String executePost(String url, Object params, String authorization) throws Exception {
        String result = null;
        String setUrl = url;
        String param = JSONObject.toJSONString(params);
        logger.info("请求url:" + url);
        logger.info("请求入参:" + param);
        HttpPost httpPost = new HttpPost(setUrl);
        httpPost.setEntity(new StringEntity(param, ContentType.create("application/json", "utf-8")));
        httpPost.setHeader("Content-Type", "application/json");
        if (StringUtils.isNotBlank(authorization)) {
            httpPost.setHeader("Authorization", authorization);
        }
        try {
            HttpResponse response = getHttpClient().execute(httpPost);
            int statusCode = response.getStatusLine().getStatusCode();
            logger.info("statusCode:" + statusCode);
            if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED || statusCode == HttpStatus.SC_BAD_REQUEST) {
                result = getStreamAsString(response.getEntity().getContent(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RRException("网络繁忙, 请稍后再试");
        }
        return result;
    }


    /**
     * 发起请求并返回结果GET
     *
     * @param url
     * @return
     * @throws Exception
     */
    public static String executeGet(String url, String authorization) {
        logger.info("请求url:" + url);
        HttpGet httpGet = new HttpGet(url);

        HttpResponse response = null;
        try {
            httpGet.setHeader("Content-Type", CONTENT_TYPE);
            if (StringUtils.isNotBlank(authorization)) {
                httpGet.setHeader("Authorization", authorization);
            }
            response = getHttpClient().execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();
            logger.info("statusCode:" + statusCode);
            if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED || statusCode == HttpStatus.SC_BAD_REQUEST) {
                return getStreamAsString(response.getEntity().getContent(), "UTF-8");
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new RRException("网络繁忙, 请稍后再试");
        }
    }


    public static String executeGetWithParam(String url, Object param, String authorization) {
        JSONObject params = (JSONObject) JSONObject.toJSON(param);
        logger.info("请求地址:{}, 请求参数:{}", url, params.toJSONString());

        StringBuffer paramsStr = new StringBuffer("?");
        String paramResult = null;
        if (!params.isEmpty()) {
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                if (entry.getValue() != null) {
                    paramsStr.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
                }
            }
            paramResult = paramsStr.substring(0, paramsStr.length() - 1);
        }

        if (paramResult != null) {
            url = url + paramResult;
        }

        String result = null;
        try {
            result = HttpCommonClientUtils.executeGet(url, authorization);
        } catch (RRException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 发起请求并返回结果get(body请求体)
     *
     * @param url
     * @return
     * @throws Exception
     */
    public static String executeGethWithBody(String url, Object params, String authorization) throws Exception {
        String result = null;
        HttpGetWithBody httpGet = new HttpGetWithBody(url);
        String param = JSONObject.toJSONString(params);
        logger.info("请求url:" + url);
        logger.info("请求入参:" + param);
        httpGet.setEntity(new StringEntity(param, ContentType.create("application/json", "utf-8")));
        httpGet.setHeader("Content-Type", CONTENT_TYPE);
        if (StringUtils.isNotBlank(authorization)) {
            httpGet.setHeader("Authorization", authorization);
        }
        try {
            HttpResponse response = getHttpClient().execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();
            logger.info("statusCode:" + statusCode);
            if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED) {
                result = getStreamAsString(response.getEntity().getContent(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RRException( "网络繁忙, 请稍后再试");
        }
        return result;
    }

    /**
     * 发起请求并返回结果PUT
     *
     * @param url
     * @param params
     * @return
     * @throws Exception
     */
    public static String executePut(String url, Object params, String authorization) throws Exception {
        String result = null;
        String setUrl = url;
        String param = JSONObject.toJSONString(params);
        logger.info("请求url:" + url);
        logger.info("请求入参:" + param);
        HttpPut httpPut = new HttpPut(setUrl);
        httpPut.setEntity(new StringEntity(param, ContentType.create("application/json", "utf-8")));
        httpPut.setHeader("Content-Type", CONTENT_TYPE);
        if (StringUtils.isNotBlank(authorization)) {
            httpPut.setHeader("Authorization", authorization);
        }
        try {
            HttpResponse response = getHttpClient().execute(httpPut);
            int statusCode = response.getStatusLine().getStatusCode();
            logger.info("statusCode:" + statusCode);
            if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED) {
                result = getStreamAsString(response.getEntity().getContent(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RRException( "网络繁忙, 请稍后再试");
        }
        return result;
    }


    /**
     * 发起请求并返回结果DELETE
     *
     * @param url
     * @param params
     * @return
     * @throws Exception
     */
    public static String executeDel(String url, Object params, String authorization) throws Exception {
        String result = null;
        String setUrl = url;
        String param = JSONObject.toJSONString(params);
        HttpDelete httpdelete = new HttpDelete(setUrl);
        httpdelete.setHeader("Content-Type", CONTENT_TYPE);
        if (StringUtils.isNotBlank(authorization)) {
            httpdelete.setHeader("Authorization", authorization);
        }
        try {
            HttpResponse response = getHttpClient().execute(httpdelete);
            int statusCode = response.getStatusLine().getStatusCode();
            logger.info("statusCode:" + statusCode);
            if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_CREATED) {
                result = getStreamAsString(response.getEntity().getContent(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RRException( "网络繁忙, 请稍后再试");
        }
        return result;
    }

    /**
     * 将流转换为字符串
     *
     * @param stream
     * @param charset
     * @return
     * @throws IOException
     */
    private static String getStreamAsString(InputStream stream, String charset) throws IOException {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream, charset), 8192);
            StringWriter writer = new StringWriter();

            char[] chars = new char[8192];
            int count = 0;
            while ((count = reader.read(chars)) > 0) {
                writer.write(chars, 0, count);
            }

            return writer.toString();
        } finally {
            if (stream != null) {
                stream.close();
            }
        }
    }

    /**
     * @param requestParam
     * @param coder
     * @return
     */
    private static String getRequestParamString(Map<String, String> requestParam, String coder) {
        if (null == coder || "".equals(coder)) {
            coder = "UTF-8";
        }
        StringBuffer sf = new StringBuffer("");
        String reqstr = "";
        if (null != requestParam && 0 != requestParam.size()) {
            for (Map.Entry<String, String> en : requestParam.entrySet()) {
                try {
                    sf.append(en.getKey()
                            + "="
                            + (null == en.getValue() || "".equals(en.getValue()) ? "" : URLEncoder
                            .encode(en.getValue(), coder)) + "&");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                    return "";
                }
            }
            reqstr = sf.substring(0, sf.length() - 1);
        }
        return reqstr;
    }
}